package cn.bdqn.pn.service.schedule.impl;

import cn.bdqn.pn.pojo.schedule.Place;
import cn.bdqn.pn.pojo.schedule.Seat;
import cn.bdqn.pn.reposity.schedule.PlaceDao;
import cn.bdqn.pn.reposity.schedule.SeatDao;
import cn.bdqn.pn.service.schedule.PlaceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Service("placeService")
@Transactional(readOnly = true)
public class PlaceServiceImpl implements PlaceService {

    /**
     * 注入场地信息
     */
    @Autowired
    private PlaceDao placeDao;

    /**
     * 注入座位信息
     */
    @Autowired
    private SeatDao seatDao;

    /**
     *新增场地
     * @param place
     * @return
     * @throws SQLException
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer addPlaceInfo(Place place) throws Exception {
      /*  System.out.println("==========================="+place.getRownum());
        System.out.println("==========================="+place.getColumnnum());*/
        Seat falseSeat=null;
        Place place1=null;
        try {
            place1=placeDao.save(place);
            //座位的总行数
            int rownum=place.getRownum();
            //座位的总列数
            int columnum=place.getColumnnum();
            for (int i=1;i<=rownum;i++){ //控制行数
                for (int j=1;j<=columnum;j++){ //控制列数
                    Seat seat=new Seat();
                    seat.setPlace(place);
                    seat.setRownum(i);
                    seat.setColumnnum(j);
                    seat.setIscorridor(1);
                    falseSeat=seatDao.save(seat);
                }
            }
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            e.printStackTrace();

        }
        if(place1!=null&&falseSeat!=null){
            return place.getPlaceid();
        }else {
            return -1;
        }
    }

    /**
     * 根据条件查询场地信息，分页
     * @param place 条件类
     * @param pageNum 当前页
     * @param pageSize 页大小
     * @return
     * @throws SQLException
     */
    @Override
    public Page<Place> getByPlaceInfoFindPlace(Place place, Integer pageNum, Integer pageSize) throws SQLException {
        Specification<Place> specification=new Specification<Place>() {
            @Override
            public Predicate toPredicate(Root<Place> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> predicateList =new ArrayList<Predicate>();
                if(place.getCityoneId()!=null && place.getCityoneId()!=0){
                    predicateList.add(cb.equal(root.get("cityoneId"),place.getCityoneId()));
                }
                if(place.getCitytwoId()!=null&&place.getCitytwoId()!=0){
                    predicateList.add(cb.equal(root.get("citytwoId"),place.getCitytwoId()));
                }
                if(place.getCitythreeId()!=null && place.getCitythreeId()!=0){
                    predicateList.add(cb.equal(root.get("citythreeId"),place.getCitythreeId()));
                }
                if(place.getStatus()!=null && place.getStatus()!=0){
                    predicateList.add(cb.equal(root.get("status"),place.getStatus()));
                }
                if(place.getAuditorstatus()!=null && place.getAuditorstatus()!=0){
                    predicateList.add(cb.equal(root.get("auditorstatus"),place.getAuditorstatus()));
                }
                if(place.getAgentid()!=null && place.getAgentid()!=0){
                    predicateList.add(cb.equal(root.get("agentid"),place.getAgentid()));
                }
                Predicate [] predicates=new Predicate[predicateList.size()];
                return cb.and(predicateList.toArray(predicates));
            }
        };
        Pageable pageable=new PageRequest(pageNum=pageNum==null?0:pageNum,pageSize=pageSize==null?5:pageSize);
        return placeDao.findAll(specification,pageable);
    }

    /**
     * 场地id修改场地信息
     * @param place
     * @return
     * @throws SQLException
     */
    @Override
    public boolean updatePlaceInfo(Place place) throws SQLException {
        if(placeDao.exists(place.getPlaceid())){
            Place placeResult=placeDao.saveAndFlush(place);
            return true;
        }
        return false;
    }

    /**
     * 根据场地id查询场地信息
     * @param placeId
     * @return
     * @throws SQLException
     */
    @Override
    public Place findByPlace(Integer placeId){
        return placeDao.findOne(placeId);
    }

    /**
     * 根据供应商查询场次信息
     * @param agentId
     * @return
     * @throws SQLException
     */
    @Override
    public List<Place> findByagentId(Integer agentId) throws SQLException {
        return placeDao.findAllByAgentid(agentId);
    }
}

